﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
namespace XRPlayer
{
    [AddComponentMenu("XR Player/XR Media Player")]
    public class XRMediaPlayer : XRBase
    {
        public FileLocation m_VideoLocation = FileLocation.RelativeToStreamingAssetsFolder;
        //public string m_VideoPath;
        [Header("Using different kinds of video targets")]
        /// <summary>
        /// Using different kinds of video targets
        /// </summary>
        public VideoTargetMode videoTargetMode = VideoTargetMode.UICanvas;

        public Platform platformUsing = Platform.Windows;

        public VideoPlayer m_VideoPlayer;
        public VideoClip m_VideoClip;
        public AudioSource m_AudioSource;
        public AudioClip m_AudioClip;
        public string m_VideoURL;
        public bool m_PlayOnAwake = false;
        public bool m_WaitForFirstFrame = false;

        public float m_WaitForSecond = 1f;
        public bool m_Loop = false;
        public float m_PlaybackSpeed;
        public RenderMode m_RendererMode;
        public VideoTime m_CurrentVideoTime;

        public XRPlayerControlLite m_XRPlayerControl;

        public XRMediaPlayer()
        {

        }
        public XRBase m_XRPlayer;
        private void Initialise()
        {
            m_XRPlayer = new XRBase();
        }
        private void Init()
        {
            m_XRPlayerControl.m_Loop = m_Loop;
            m_XRPlayerControl.m_WaitForSecond = m_WaitForSecond;
        }
        private void Awake()
        {
            m_XRPlayerControl.m_VideoCanvas.AddComponent<AudioSource>();
            m_XRPlayerControl.m_VideoCanvas.AddComponent<VideoPlayer>();
            m_AudioSource = m_XRPlayerControl.m_VideoCanvas.GetComponent<AudioSource>();
            m_VideoPlayer = m_XRPlayerControl.m_VideoCanvas.GetComponent<VideoPlayer>();
            m_VideoPlayer.playOnAwake = false;
            m_AudioSource.playOnAwake = false;
            m_XRPlayerControl.videoPlayer = m_VideoPlayer;
            m_XRPlayerControl.audioPlayer = m_AudioSource;
            m_VideoPlayer.clip = m_VideoClip;
            Init();
            switch (platformUsing)
            {
                case Platform.Windows:
                    break;
                case Platform.Android:
                    break;
                case Platform.IOS:
                    break;
                case Platform.ARKit:
                    break;
                case Platform.ARCore:
                    break;
                case Platform.Vuforia:
                    break;
                case Platform.VR:
                    break;
                default:
                    break;
            }

            switch (videoTargetMode)
            {
                case VideoTargetMode.UICanvas:
                    m_VideoPlayer.renderMode = VideoRenderMode.RenderTexture;
                    m_VideoPlayer.targetTexture = m_XRPlayerControl.m_VideoCanvas.
                        GetComponent<RawImage>().texture as RenderTexture;
                    break;
                case VideoTargetMode.SphereVR:
                    break;
                case VideoTargetMode.Plane:
                    break;
                case VideoTargetMode.Outside:
                    break;
                default:
                    break;
            }
            if (m_VideoClip == null)
                return;
            m_VideoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
            m_VideoPlayer.SetTargetAudioSource(0, m_AudioSource);

        }

        public override bool OpenVideoFromFile(string path)
        {
            //            string url = Application.streamingAssetsPath + "/" + path;
            //#if UNITY_EDITOR || UNITY_STANDALONE
            //            //string readStr = File.ReadAllText(url);
            //            m_VideoPlayer.clip = 
            //            byte[] readStr = File.ReadAllBytes(url);
            //#elif UNITY_ANDROID
            //            WWW www = new WWW(url);
            //            while (!www.isDone) { }
            //            readStr = www.text;
            //#endif
            //            if (readStr != null)
            //            {
            //                return true;
            //            }
            //            else
            //            {
            //                return false;
            //            }
            return false;
        }

    }
    //Resource location
    public enum FileLocation
    {
        AbsolutePathOrURL,
        RelativeToProjectFolder,
        RelativeToStreamingAssetsFolder,
        RelativeToDataFolder,
        RelativeToPeristentDataFolder,
    }
    public struct VideoTime
    {
        public int hour;
        public int minute;
        public int second;
        /// <summary>
        /// Show the format time in "00:00:00"
        /// </summary>
        public string ShowTimeString;
    }
    //public struct VideoClipInformation
    //{
    //    public float VideoWidth;
    //    public float VideoHeight;
    //    public string VideoTypeName;
    //}
    public enum VideoTargetMode
    {
        UICanvas,
        SphereVR,
        Plane,
        Outside,
    }

}